/*
 * RequestParameter.java
 *
 * Created on 2006년 3월 31일 (금), 오후 5:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.zll.framework.model;


import java.io.File;

import java.util.*;
import java.text.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import com.zll.framework.util.*;
import com.zll.framework.conf.Configuration;
import com.zll.framework.db.*;

/**
 * @author eastar
 */
public class HashBox
{
	private HttpServletRequest request;
	private HashMap param;				//Request에 설정된 parameter 정보
	private HashMap listMap;			//Request에 parameterValues가 여러개 존재할 경우. 각가 HashList로 변환하여 저장


	public String work			= "";
   	public String menuid		= "";
   	public String target		= "";
   	public String searchParam	= "";			//Request에 설정된 검색조건('s_' 로 시작)을 파라미터 스트링으로 저장.

	public HashBox() {
		this.param = new HashMap();
	}

	public HashBox(HttpServletRequest request) throws Exception
	{
		HashMap map		= new HashMap();
		this.listMap 	= new HashMap();
		String name 	= "";
		String value	= "";

		String userid	= StringUtil.nvl(request.getSession().getAttribute("session.user_id"));
		if ("".equals(userid)) {
			userid = StringUtil.nvl(request.getSession().getAttribute("session.member_id"));
		}

    	String contentType	= StringUtil.nvl(request.getContentType());

    	boolean isMultiPart = (contentType.indexOf("multipart/form-data") > -1) ? true : false;


		if (isMultiPart)
		{
			//String imgPath				= Configuration.fileDir + "temp"; // 저장할 임시 디렉토리 (절대경로)
			//String tempPath				= Configuration.fileDir + "temp" + File.separator; // 저장할 임시 디렉토리 (절대경로)
			int sizeLimit 				= 100 * 1024 * 1024 ; // 5메가까지 제한 넘어서면 예외발생

			MultipartRequest multi		= new MultipartRequest(request, Configuration.get("dir.temp"), sizeLimit, "utf-8", new DefaultFileRenamePolicy());

			Enumeration e 	= multi.getParameterNames();
			while (e.hasMoreElements())
			{
				//System.out.println("▷▷▷▷▷▷"+name+":" + multi.getParameter(name));

				name 	= StringUtil.nvl(e.nextElement());


				if (name.indexOf('.') > -1) {
					String listName 	= name.substring(0, name.indexOf('.'));
					String paramName	= name.substring(name.indexOf('.')+1);
					String[] arr		= multi.getParameterValues(name);

					boolean isExists	= listMap.containsKey(listName);

					for (int i = 0; i < arr.length; i++) {
						if (isExists) {
							HashList list = (HashList)listMap.get(listName);
							list.put(paramName, i, StringUtil.nvl(arr[i]));
						} else {
							HashList list = new HashList();
							list.put(paramName, i, StringUtil.nvl(arr[i]));
							listMap.put(listName, list);
							isExists = true;
						}
					}
				} else {

					// reserved parameter
	    			if ("work".equals(name)) {
	    				this.work = StringUtil.nvl(multi.getParameter("work"));
	    			} else if ("menuid".equals(name)) {
	    				this.menuid = StringUtil.nvl(multi.getParameter("menuid"));
	    			} else if ("target".equals(name)) {
	    				this.target = StringUtil.nvl(multi.getParameter("target"));
	    			// general parameter
	    			} else {
						value 	= multi.getParameter(name);
						map.put(name, value);
						if (name.indexOf("s_") == 0) {
							if ("".equals(this.searchParam)) {
								this.searchParam += name + "=" + StringUtil.nvl(value);
							} else {
								this.searchParam += "&" + name + "=" + StringUtil.nvl(value);
							}
						}
					}
				}
			}


			Enumeration formNames 		= multi.getFileNames();
			while (formNames.hasMoreElements()) {

				String formName = (String)formNames.nextElement();
				File f	= multi.getFile(formName);

				if (f != null && f.isFile()) {
					System.out.println(f.getName());
					//map.put(formName, savePath + f.getName());
					map.put("file_"+formName, f);
				}
			}



		}
		else {
			Enumeration e 	= request.getParameterNames();
			while (e.hasMoreElements())
			{
				name 	= StringUtil.nvl(e.nextElement());
				//System.out.println("▷▷▷▷▷▷"+name+":" + request.getParameter(name));
				if (name.indexOf('.') > -1) {
					String listName 	= name.substring(0, name.indexOf('.'));
					String paramName	= name.substring(name.indexOf('.')+1);
					String[] arr		= request.getParameterValues(name);

					boolean isExists	= listMap.containsKey(listName);

					for (int i = 0; i < arr.length; i++) {
						if (isExists) {
							HashList list = (HashList)listMap.get(listName);
							list.put(paramName, i, StringUtil.nvl(arr[i]));
						} else {
							HashList list = new HashList();
							list.put(paramName, i, StringUtil.nvl(arr[i]));
							listMap.put(listName, list);
							isExists = true;
						}
					}
				} else {
					// reserved parameter
	    			if ("work".equals(name)) {
	    				this.work = StringUtil.nvl(request.getParameter("work"));
	    			} else if ("menuid".equals(name)) {
	    				this.menuid = StringUtil.nvl(request.getParameter("menuid"));
	    			} else if ("target".equals(name)) {
	    				this.target = StringUtil.nvl(request.getParameter("target"));
	    			// general parameter
	    			} else {
						value 	= request.getParameter(name);
						map.put(name, StringUtil.nvl(value));
						if (name.indexOf("s_") == 0) {
							if ("".equals(this.searchParam)) {
								this.searchParam += name + "=" + StringUtil.nvl(value);
							} else {
								this.searchParam += "&" + name + "=" + StringUtil.nvl(value);
							}
						}
					}
				}
			}
		}


		//session userid
		map.put("init_user", userid);
		map.put("up_user", userid);

		Iterator iter = listMap.keySet().iterator();

  		while (iter.hasNext()) {
   			HashList list = (HashList)listMap.get((String)iter.next());
   			for (int i = 0; i < list.size(); i++) {
   				list.set("init_user", i, userid);
   				list.set("up_user", i, userid);
   			}
		}


		this.param 			= map;
		this.request 		= request;
	}



	public HashList getList() {
		return getList("list");
	}

	public HashList getList(String listName) {
		HashList list = (HashList)listMap.get(listName);
		if (list == null) {
			return new HashList();
		} else {
			return list;
		}
	}


	public int size() {
		return param.size();
	}

	public HashMap getParam() {
		return param;
	}

	public void setParam(HashMap param) {
		this.param = param;
	}

	public String get(String key) {
		return StringUtil.nvl(param.get(key));
	}

	public String getNull(String key) {
		return (String)param.get(key);
	}

	public int getInt(String key) {
		String value = StringUtil.nvl(param.get(key),"0");
		return Integer.parseInt(value);
	}

	public File getFile(String key) {
		return (File)param.get("file_"+key);
	}

	public void set(String key, String value) {
		param.put(key, value);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void put(String key, Object value) {
		this.param.put(key, value);
	}
}


